package com.pets.springboot_petshop.controller;

import com.pets.springboot_petshop.entity.Appointmentslot;
import com.pets.springboot_petshop.entity.Order;
import com.pets.springboot_petshop.entity.Orderproduct;
import com.pets.springboot_petshop.entity.Service;
import com.pets.springboot_petshop.pojo.Result;
import com.pets.springboot_petshop.service.AppointmentslotService;
import com.pets.springboot_petshop.service.ServiceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;

/**
 * @Author: wxk
 * @Email: 2089967794@qq.com
 * @Date: 2024/10/14 下午3:21 周一
 * @Description: 预约时间和名额控制器
 */
@RestController
@RequestMapping("/Appointmentslot")
public class AppointmentslotController {
    @Autowired
    private AppointmentslotService appointmentslotService;
    @Autowired
    private ServiceService serviceService;

@GetMapping("/select")
private Result selectAppointmentslotServiceid(@RequestParam Integer id) {
    Appointmentslot appointmentslot = appointmentslotService.selectappointmentslot(id);
    if (appointmentslot == null) {
        return Result.error("获取失败");
    }else {
        return Result.success(appointmentslot);
    }
}
    @GetMapping("/selectservice_id")
    private Result selectservice_id(@RequestParam Integer service_id) {
        List<Appointmentslot> appointmentslot = appointmentslotService.selectservice_id(service_id);
        if (appointmentslot == null) {
            return Result.error("获取失败");
        }else {
            return Result.success(appointmentslot);
        }
    }
    //service_name，通过服务分类查询
    @GetMapping("/selectname")
    private Result selectname(@RequestParam String name) {
        // 直接通过服务名称查询预约时间槽
        List<Appointmentslot> appointmentslot = appointmentslotService.selectname(name);

        if (appointmentslot == null || appointmentslot.isEmpty()) {
            // 如果列表为空或为null，返回错误状态
            return Result.error("没有找到相关产品");
        } else {
            // 如果列表不为空，返回产品列表
            return Result.success(appointmentslot);
        }
    }
    //查询所有
    @GetMapping("/findAll")
    private Result findAll() {
        List<Appointmentslot> services = appointmentslotService.findAll();
        if(services == null || services.isEmpty()){
            return Result.error("获取失败");
        }else {
            return Result.success(services);
        }
    }
    // 根据日期查询
    @GetMapping("/selectOrder")
    public Result selectOrder(@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate date) {
        // 获取订单
        List<Appointmentslot> appointmentslots = appointmentslotService.selectOrder(date);
        if (appointmentslots != null) {
            return Result.success(appointmentslots);
        } else {
            return Result.error("获取失败");
        }
    }
    //管理员添加
    @PostMapping("/add")
    private Result addService(@RequestBody Appointmentslot appointmentslot) {
        if (appointmentslotService.addAppointmentslot(appointmentslot) > 0) {
            return Result.success("新增成功");
        } else {
            return Result.error("新增失败");
        }
    }
    //删除
    @PostMapping("/delete")
    private Result deleteServiceById(@RequestParam Integer id) {
        if( appointmentslotService.deleteAppointmentslotById(id) > 0){
            return Result.success();
        }else {
            return Result.error("删除失败");
        }
    }
    //修改数据
    @PostMapping("/update")
    private Result updateServiceById(@RequestBody Appointmentslot appointmentslot) {
        if (appointmentslotService.updateAppointmentslotById(appointmentslot) > 0) {
            return Result.success();
        } else {
            return Result.error("修改失败");
        }
    }
    @PostMapping("/batchUpdateStatus")
    public Result batchUpdateStatus(@RequestBody Map<String, Object> payload) {
        // 从payload中获取orderIds和status
        List<Integer> orderIdList = (List<Integer>) payload.get("ids");
        String newState = (String) payload.get("state");
        // 将List转换为int数组
        int[] ids = new int[orderIdList.size()];
        for (int i = 0; i < orderIdList.size(); i++) {
            ids[i] = orderIdList.get(i);
        }

        // 调用service层方法执行批量更新
        int updated = appointmentslotService.batchUpdateStatus(ids, newState);
        return updated > 0 ? Result.success("批量更新状态成功") : Result.error("批量更新状态失败");
    }
    @PostMapping("/admindelete")
    public Result admindelete(@RequestBody Map<String, Object> payload) {
        try {
            // 从payload中获取orderIds
            List<Integer> orderIdList = (List<Integer>) payload.get("ids");
            if (orderIdList == null || orderIdList.isEmpty()) {
                return Result.error("未提供有效的ID列表");
            }

            // 将List转换为int数组
            int[] ids = new int[orderIdList.size()];
            for (int i = 0; i < orderIdList.size(); i++) {
                ids[i] = orderIdList.get(i);
            }

            int affectedRows = appointmentslotService.admindelete(ids);
            if (affectedRows > 0) {
                return Result.success("删除成功");
            } else {
                return Result.error("删除失败");
            }
        } catch (ClassCastException e) {
            return Result.error("ID列表格式错误，应为整数数组");
        } catch (Exception e) {
            return Result.error("服务器错误：" + e.getMessage());
        }
    }
}
